/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: Frank
 * 系统版本: 1.0.0
 * 开发人员: Frank
 * 开发时间: 2019/10/31 13:15
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.oauth2.handler;

import com.spring.arch.common.security.SecurityLog;
import com.spring.arch.common.utils.HttpUtils;
import com.spring.arch.common.security.SecurityLog;
import com.spring.arch.common.utils.HttpUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证入口配置
 * @author Frank
 * @version 1.0.0.1
 * @since 2019/10/31 13:15
 */
public class SecurityAuthenticationEntryPoint implements AuthenticationEntryPoint {

    /**
     * 入口程序
     * 认证失败响应信息封装
     * @param request   请求
     * @param response  响应
     * @param e 认证异常
     */
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException {
        final String clientIp = HttpUtils.getClientIP(request);

        SecurityLog.authentication.warn(
                "[{}]JWT Authentication failed: {} {}, {}",
                clientIp,
                request.getMethod(),
                request.getRequestURI(),
                e.getMessage());

        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, e.getMessage());
    }
}
